package wildcard.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wildcard.database.DBAccess;

public class MaintenanceExecuteServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		ArrayList<ArrayList<String>> resultList = null;
		ArrayList<String> parameters = new ArrayList<String>(0);

		// HTTPの設定
		req.setCharacterEncoding("UTF-8");
		HttpSession session = req.getSession();

		// ユーザIDの取得
		LoginBean loginBean = (LoginBean) session.getAttribute("LoginUser");
		String dbId = loginBean.getLoginId().substring(0, 2);
		String dbPassword = loginBean.getPassword();

		// 遷移先情報
		String pagetype = req.getParameter("pagetype");

		// SQL文を取得するためにデータベースにアクセスする
		String sqlId = req.getParameter("sql");
		parameters.add(sqlId);
		resultList = DBAccess.executeSelect(dbId, dbPassword,
				DBAccess.SQL_SELECT_COMMAND, parameters);

		// アクセスした結果を格納する
		String sql = resultList.get(1).get(0);
		System.out.println("SQL: " + sql);

		// 目的のレコードを取得
		parameters = null;
		resultList = DBAccess.executeSelect(dbId, dbPassword, sql, parameters);

		// SQL25は表示が特殊であるためリストを加工する
		if ("SQL25".equals(sqlId)) {
			resultList = createResultSQL25(resultList);
		}

		// 出力リストを次のページへ送る
		req.setAttribute("result", resultList);

		// 画面の遷移
		RequestDispatcher dispatcher;

		// 遷移先の決定
		if ("report".equals(pagetype)) {
			req.setAttribute("title", req.getParameter("title"));
			dispatcher = req.getRequestDispatcher("report.jsp");
		} else {
			session.setAttribute("target", req.getParameter("target"));
			session.setAttribute("title", req.getParameter("title"));
			dispatcher = req.getRequestDispatcher("maintenance_edit.jsp");
		}
		dispatcher.forward(req, resp);
	}

	private ArrayList<ArrayList<String>> createResultSQL25(
			ArrayList<ArrayList<String>> resultList) {
		ArrayList<ArrayList<String>> newList = new ArrayList<ArrayList<String>>();
		ArrayList<String> newRecord = null;

		for (int row = 0; row < resultList.size() / 2 + 1; row++) {
			newRecord = new ArrayList<String>();
			// 列名は特殊処理
			if (row == 0) {
				ArrayList<String> oldRecord = resultList.get(0);
				for (int column = 0; column < oldRecord.size() * 2; column++) {
					String str = oldRecord.get(column % oldRecord.size());
					newRecord.add(str);
				}
			} else {
				for (int i = row; i < resultList.size(); i += resultList.size() / 2) {
					ArrayList<String> oldRecord = resultList.get(i);
					for (int column = 0; column < oldRecord.size(); column++) {
						String str = oldRecord.get(column);
						newRecord.add(str);
					}
				}
			}
			newList.add(newRecord);
		}
		return newList;
	}
}
